/**
 * Created by shitao on 15/11/2.
 */
(function () {
    angular.module('my-tabs.controllers')
        .controller('friendCtrl', friendCtrl);
    friendCtrl.$inject = ['$scope', 'friendService', '$state', '$ionicScrollDelegate', '_'];

    function friendCtrl($scope, friendService, $state, $ionicScrollDelegate, _) {
        console.log('friendCtrl');
        var dragScale, friendList, barHeight, tabNav, actionBtn, barHeight, body;
        dragScale = 3;
        friendList = angular.element(document.querySelector('.friend-list .list'));
        barHeader = angular.element(document.querySelectorAll('.bar-header-box > .bar-header'));
        tabNav = angular.element(document.querySelector('.tab-nav'));
        actionBtn = angular.element(document.querySelector('.action-btn-box > .row'));
        body = angular.element(document.querySelector('body'));
        if (body.hasClass('platform-ios') && body.hasClass('platform-cordova')) {
            barHeight = 64;
        } else {
            barHeight = 44;
        }

        var vm = this;
        vm.status = 'list';
        vm.primaryBtnText = '选取';
        vm.friends = friendService.all();
        vm.dragging = false;
        vm.selectCount = 0;
        vm.dragDistance = 0;
        vm.openDialog = function (friend) {
            $state.go('main.friend-dialog', {friendId: friend.id});
        };
        vm.toggleSelect = function (friend) {
            friend.selected = !friend.selected;
            if (friend.selected) {
                vm.selectCount++;
            } else {
                vm.selectCount--;
            }
        };
        vm.toggleStatus = function (status) {
            vm.status = status || (vm.status === 'list' ? 'selection' : 'list');
        };
        vm.enterSelect = function(){
            vm.toggleStatus('selection');
        };
        vm.cancelSelect = function () {
            _.each(vm.friends, function (friend) {
                friend.selected = false;
            });
            vm.selectCount = 0;
            vm.toggleStatus('list');
        };
        vm.toggleAll = function () {
            if (vm.selectCount === vm.friends.length &&
                vm.selectCount != 0) {
                _.each(vm.friends, function (friend) {
                    friend.selected = false;
                });
                vm.selectCount = 0;
            } else {
                _.each(vm.friends, function (friend) {
                    friend.selected = true;
                });
                vm.selectCount = vm.friends.length;
            }
        };
        vm.onItemClick = function (friend) {
            if (vm.status === 'list') {
                vm.openDialog(friend);
            } else {
                vm.toggleSelect(friend);
            }
        };
        vm.onDragRight = ionic.throttle(
            function (event) {
                vm.dragging = true;
                var distance = Math.floor(event.gesture.distance / dragScale);
                if(vm.dragDistance < 50) {
                    vm.dragDistance = Math.min(distance, 50);
                }
            },50);
        vm.onDragLeft = ionic.throttle(
            function (event) {
                vm.dragging = true;
                var distance = Math.floor(event.gesture.distance / dragScale);
                if(vm.dragDistance > 0) {
                    vm.dragDistance = Math.max(50 - distance, 0);
                }
            }, 50);
        vm.onRelease = function () {
            if (vm.dragging === true) {
                vm.dragging = false;
                if (vm.dragDistance < 30) {
                    vm.dragDistance = 0;
                    vm.status = 'list';
                } else {
                    vm.dragDistance = 50;
                    vm.status = 'selection';
                }
            }
        };
        $scope.$watch('vm.status', function (newVal) {
            vm.primaryBtnText = newVal === 'list' ? '选取' : '取消';
            if (newVal === 'list') {
                vm.dragDistance = 0;
            } else {
                vm.dragDistance = 50;
            }
        });
        $scope.$watch('vm.selectCount', function (newVal) {
            if (newVal !== vm.friends.length || newVal == 0) {
                vm.selectAllBtnText = '全选';
                if (newVal == 0) {
                    vm.selectTitle = '多选';
                } else {
                    vm.selectTitle = '已选择' + vm.selectCount + '条';
                }
            } else {
                vm.selectAllBtnText = '取消全选';
                vm.selectTitle = '已选择' + vm.selectCount + '条';
            }
        });
        $scope.$watch('vm.dragging', function (newVal) {
            var friendContent = $ionicScrollDelegate.$getByHandle('friend-content');
            if (newVal === true) {
                friendContent.freezeScroll(true);
            } else {
                friendContent.freezeScroll(false);
            }
        });
        $scope.$watch('vm.dragDistance', function (newVal) {
            // 计算列表平移的distance
            var listDistance = newVal - 50;
            // 计算头部bar上下移动的distance
            var barDistance = -Math.floor(newVal * barHeight / 50);
            // 计算底部tab上下移动的distance
            var tabDistance = newVal;
            // 计算底部actionBar上下移动的distance
            var actionDistance = tabDistance - 50;

            friendList.css('transform', 'translate3d(' + listDistance + 'px, 0, 0)');
            barHeader.css('transform', 'translate3d(0, ' + barDistance + 'px, 0)');
            tabNav.css('transform', 'translate3d(0, ' + tabDistance + 'px, 0)');
            actionBtn.css('transform', 'translate3d(0, ' + actionDistance + 'px, 0)');
        });
    }
})();
